//AB32 【模板】哈夫曼编码
import java.util.;

注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
        注意 hasNext 和 hasNextLine 的区别
       while (in.hasNextInt()) {  注意 while 处理多个 case
           int n = in.nextInt();
           PriorityQueueLong pq = new PriorityQueue();
           for (int i = 0; i  n; i++) {
               pq.add(in.nextLong());
           }
           long sum = 0;
           while (pq.size()  1) {
               long a1 = pq.poll();
               long a2 = pq.poll();
               sum += a1;
               sum += a2;
               pq.add(a1+a2);
           }
           System.out.println(sum);
       }
   }
}